#coding=utf-8
import urllib, urllib2

def print_red(s):
  print '\033[0;31;48m%s\033[0m'%s  
  
import os, sys, subprocess
SCRIPT_ROOT = os.path.realpath(os.path.dirname(__file__))
PACKAGE_PATH = os.path.normpath(os.path.join(SCRIPT_ROOT, '../../'))
sys.path.append(PACKAGE_PATH)

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'emailserver_manager.settings'
from emailserver_manager.host.models import App, Account

apps = App.objects.order_by('created_at').all()
for app in apps:  
  EMAILHTML = 'email1.html'
  email_html = open(EMAILHTML).read()
  SUBJECT = '${name}:-)春天到，翻东东购物网为你翻出了淘宝上热卖好东东!'

  url = 'http://%s.appspot.com/receive_ad'%app.name
  print url
  values = {'campaign': '春装',
            'subject': SUBJECT,
            'plain': '$email 春天到，看看翻东东购物网为你精选淘宝上热卖的春装吧!',
            'html': email_html}

  data = urllib.urlencode(values)
  req = urllib2.Request(url, data)
  response = urllib2.urlopen(req)
  print_red(app.name)
  the_page = response.read()
  print the_page[:10]
  
